# rm(list=ls()) # remove when done debugging
daily.SP<-read.table("../data/S&P500_Daily_Data.csv", sep=",", skip=1, header=TRUE, stringsAsFactors=FALSE)
daily.SP<-daily.SP[1:5040,] # our daily data extends past project monthly data, so trim
daily.SP<-data.frame(daily.SP, month.end = rep(FALSE,nrow(daily.SP))) #add extra column
# find rows where first 2 chars of Date column do not match the next column
for(i in 1:nrow(daily.SP))
{
  # R prefers the use of identical() instead of ==, espcially in loops
  if(!identical(substr(daily.SP$Date[i],1,2),substr(daily.SP$Date[i+1],1,2)))
  {
    daily.SP$month.end[i] = TRUE # mark the end of the month
  }
}
monthly.SP<-daily.SP[daily.SP$month.end==TRUE,-ncol(daily.SP)]
write.table(monthly.SP,file="../data/m_SP500.csv",sep=",", row.names=FALSE) #print to file